home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- *
- * Apple Developer Technical Support
- *
- * Text section handling routines
- *
- * Program: EditionSample
- * File: Windows.c - C Source
- *
- * by: C.K. Haun <TR>
- *
- * Copyright © 1990,1991 Apple Computer, Inc.
- * All rights reserved.
- *
- *------------------------------------------------------------------------------
- * This file contains some of our window handling routines. I created it
- * primarily to take some of the clutter out of the main.c file.
- * It also contains the clipboard handlers
- *----------------------------------------------------------------------------*/
-
- #define __MYWINDOWS__
-
- #pragma segment MyWindows
- #pragma load "EdSampheaders" /* see the Buildheaders.c file */
-
- #include "EdSampdefines.h"
- /* protos for this file */
- void ChangePlane(WindowPtr twindow);
- Boolean IsAppWindow(WindowPtr window);
- Boolean IsDAWindow(WindowPtr window);
- WindowPtr AddNewWindow(Boolean showIt);
- void CloseMyWindow(WindowPtr theClose);
-
- /* clipboard stuff */
- void DrawClip(WindowPtr windowIn);
- void ClipClick(WindowPtr twindow);
- void CloseClip(void);
- void UpdateScrap(void);
- void SpitClip(void);
-
-
-
- /* external references */
- extern MenuHandle gAppleMenuHandle, gFileMenuHandle, gEditMenuHandle, gToolMenuHandle, gAdditionalMenu;
- extern WindowPtr gCurrentWindow; /* window currently frontmost */
- extern WindowPtr gClipWindowPtr; /* our clipboard window */
- extern WindowPtr gActionWindows; /* start of our window list */
- extern short gWindowCount; /* current number of windows, since I'm restricting to 10 */
- extern Boolean gStop; /* Stop flag for this app */
- extern unsigned long gMasterWindowID; /* for section tracking */
- extern Rect gShowPubRect; /* rectangle of the currently selected publisher */
- extern Rect gShowSubRect; /* rectangle of the currently selected subscriber */
- extern SectionHandle gShowingSecHandle; /* currently selected section */
- extern SectionHandle gClipSection; /* if the clipboard contains a section */
- extern PicHandle gClipPict; /* for the section picture */
- extern Boolean gShowPub; /* telling if a publisher or subscriber */
- extern Boolean gShowSub; /* border should be displayed */
- extern Boolean gShowingAll; /* show all borders toggle */
- extern Boolean gInBackground; /* Where Are We? */
- extern Boolean gExpanded; /* flag for expanded dialogs, for this sample */
- extern Boolean gResizeSub; /* resize flag for this sample */
- extern short gClipHasContents; /* indicates the contents of the clipboard (PICT, TEXT, section ) */
- extern short actsToIDs[];
- extern void DrawFull(windowCHandle theWind, WindowPtr theWindPtr);
- extern void SaveMe(windowCHandle theWind, WindowPtr theWindPtr);
- extern void DoDocumentClick(WindowPtr twindow);
- extern Handle gScrapData;
-
-
-
-
-
-
- /* AddNewWindow creates and initializes a new document window. */
- /* It returns false in this example if there are already 10 windows open. */
- WindowPtr AddNewWindow(Boolean showIt)
- {
- windowCHandle setControls;
- WindowPtr tempWP;
- Handle tempHand;
- short cnt = 0;
- Str31 wtitle;
- if (gWindowCount > 9)
- return(false);
- tempWP = GetNewWindow(kDocWindow, 0, (WindowPtr)-1); /* get a new window */
- /* and add it to our list by finding the current last window in the chain */
- if (gActionWindows == nil) { /* there are no windows yet */
- gActionWindows = tempWP;
- } else {
- Boolean fred = true;
- setControls = (windowCHandle)GetWRefCon(gActionWindows);
- do {
- if ((*setControls)->nextWindow == nil)
- fred = false;
- else
- setControls = (windowCHandle)((WindowPeek)(*setControls)->nextWindow)->refCon;
- } while (fred);
- (*setControls)->nextWindow = tempWP;
- }
- gCurrentWindow = tempWP; /* make it the current window */
- tempHand = NewHandle(sizeof(windowControl)); /* add our control structure to it */
- ((WindowPeek)tempWP)->refCon = tempHand;
- setControls = (windowCHandle)((WindowPeek)tempWP)->refCon; /* and put it where we can use it */
- HLock((Handle)setControls); /* lock it down */
- /* add pointers to our procedures for drawing, saving, and closing */
- (*setControls)->drawMe = (ProcPtr)DrawFull;
- (*setControls)->saveMe = (ProcPtr)SaveMe;
- (*setControls)->closeMe = (ProcPtr)CloseMyWindow;
- (*setControls)->clickMe = (ProcPtr)DoDocumentClick;
- /* now initialize all our required handles, and move 'em hi */
- (*setControls)->lineList = (myLineHandle)NewHandle(0);
- (*setControls)->rectList = NewHandle(0);
- (*setControls)->ovalList = NewHandle(0);
- (*setControls)->pubs = NewHandle(0);
- (*setControls)->pubRects = NewHandle(0);
- (*setControls)->numPubs = 0;
- (*setControls)->subs = NewHandle(0);
- (*setControls)->subRects = NewHandle(0);
- (*setControls)->subDataHandle = NewHandle(0);
- (*setControls)->numSubs = 0;
- (*setControls)->pictHandle = NewHandle(0);
- (*setControls)->pictRects = NewHandle(0);
- (*setControls)->numPicts = 0;
- (*setControls)->nextWindow = nil;
- /* initialize our control codes and counts */
- (*setControls)->lineCount = 0;
- (*setControls)->rectCount = 0;
- (*setControls)->ovalCount = 0;
- (*setControls)->currentAction = 0;
- (*setControls)->undoAction = 0;
- (*setControls)->hasSelection = false;
- (*setControls)->windowDirty = false;
- (*setControls)->windowID = gMasterWindowID;
- gMasterWindowID += 10000; /* update for next window */
- /* clear the file spec for the file this document will be saved to */
- (*setControls)->fileAliasHandle = (AliasHandle)NewHandle(0);
- (*setControls)->boxHandle = nil;
- (*setControls)->textSections = nil;
- /* if there already is a window (or more) add a count to this title */
- if (showIt) {
- GetWTitle(tempWP, wtitle);
- if (gWindowCount) {
- wtitle[9] = gWindowCount | 0x30; /* make number ascii */
-
- } else { /* take off the trailing space */
- wtitle[0]--;
- }
- SetWTitle(tempWP, wtitle);
- ShowWindow(tempWP); /* show it */
- }
- HUnlock((Handle)setControls);
- SetPort(tempWP);
- gWindowCount++;
- SetUndo(0, false); /* since it is the current window, set all the actions to default */
- SetCurAction(0);
- SetMyCursor(0);
- SwitchChecks(0);
- SetWMenus(true);
- if (gWindowCount > 9) {
- DisableItem(gFileMenuHandle, kOpenItem);
- DisableItem(gFileMenuHandle, kNewItem);
- }
- return(tempWP);
- }
-
- /* end AddNewWindow */
-
- /* CloseMyWindow closes the window and disposes of all the memory
- * associated with it. It also calls HandleSections (sic) to UnRegister
- * all the editions associated with this window
- */
-
- void CloseMyWindow(WindowPtr theClose)
- {
- windowCHandle theWind;
- windowCHandle tempWC;
- WindowPtr tempWP;
- short alBack;
- register qq;
- PicHandle *tempPicHandPtr;
- theWind = (windowCHandle)GetWRefCon(theClose);
- HLock((Handle)theWind);
- if ((*theWind)->windowDirty) {
- Str255 windName;
- GetWTitle(theClose, windName);
- ParamText(windName, "", "", "");
- alBack = Alert(kDirtyAlert, nil);
- if (alBack == kOK)
- (ProcPtr)(*(theWind))->saveMe(theWind, theClose);
- if (alBack == kCancel) {
- gStop = false;
- return;
- }
- }
- DisposHandle((*theWind)->rectList);
- DisposHandle((*theWind)->ovalList);
- DisposHandle((Handle)(*theWind)->lineList);
- HandleSectionSave(theWind, false, true, nil);
- DisposHandle((*theWind)->pubs);
- DisposHandle((*theWind)->pubRects);
- DisposHandle((*theWind)->subs);
- DisposHandle((*theWind)->subRects);
- DisposHandle((Handle)(*theWind)->fileAliasHandle);
- if ((*theWind)->boxHandle != nil)
- TEDispose((*theWind)->boxHandle);
- /* if there are any pictures, kill them too */
- if ((*theWind)->numPicts) {
- HLock((*theWind)->pictHandle);
- tempPicHandPtr = (PicHandle *)*(*theWind)->pictHandle;
- for (qq = 0; qq < (*theWind)->numPicts; qq++) { /* rectangles are handled in the HandleSectionSave */
- KillPicture(*tempPicHandPtr);
- tempPicHandPtr += 1;
- }
- }
- DisposHandle((*theWind)->pictHandle);
- DisposHandle((*theWind)->pictRects);
-
- /* find this window in the list, remove it and remake the link */
- if (gActionWindows != theClose) {
- Boolean fred = true;
- tempWP = gActionWindows;
- do {
- tempWC = (windowCHandle)GetWRefCon(tempWP);
- if ((*tempWC)->nextWindow == theClose)
- fred = false;
- else
- tempWP = (*tempWC)->nextWindow;
- } while (fred);
- (*tempWC)->nextWindow = (*theWind)->nextWindow;
- } else {
- /* first in the list, just kill this one */
- gActionWindows = (*theWind)->nextWindow; /* may be nil, that's fine */
- tempWP = gActionWindows;
- }
- /* and get rid of this one */
- DisposHandle((Handle)theWind);
- CloseWindow(theClose);
- gWindowCount--;
- gCurrentWindow = tempWP;
- if (tempWP != nil) {
- SelectWindow(tempWP);
- SetPort(tempWP);
- gShowingSecHandle = nil;
- gShowPub = false;
- gShowSub = false;
- } else {
- gCurrentWindow = nil;
- SetWMenus(false);
- gShowingSecHandle = nil;
- gShowPub = false;
- gShowSub = false;
- }
- EnableItem(gFileMenuHandle, kOpenItem);
- EnableItem(gFileMenuHandle, kNewItem);
-
- }
-
-
-
-
- /* ChangePlane does the housekeeping we need to do when a window comes to */
- /* the front, swapping menu checks, setting the cursor, and so on */
- void ChangePlane(WindowPtr twindow)
- {
- short temp;
- windowCHandle shortname;
- /* Before the swap kill any current borders */
- DeBorderSelection();
- SelectWindow(twindow); /* select the window */
- /* deactivate the textedit record of the old window, if there is one */
- if (gCurrentWindow != nil) {
- windowCHandle tempCH = (windowCHandle)GetWRefCon(gCurrentWindow);
- if ((*tempCH)->boxHandle != nil)
- TEDeactivate((*tempCH)->boxHandle);
- }
- if (gCurrentWindow == gClipWindowPtr) {
- SetWMenus(true);
- /* turn tool menus back on if last window was clipbaord */
- }
- gCurrentWindow = twindow; /* make it our current window */
- SetUndo(0, true); /* tell SetUndo to pull the undo item value out of the current record */
- shortname = (windowCHandle)GetWRefCon(gCurrentWindow);
- HLock((Handle)shortname);
- temp = (*shortname)->currentAction; /* pull the last action the */
- HUnlock((Handle)shortname); /* user was performing out of the */
- SetCurAction(temp); /* window record */
- SetMyCursor(actsToIDs[temp]); /* set the right cursor */
- SwitchChecks(actsToIDs[temp]); /* check the right menu item */
- gShowPub = gShowSub = false; /* selections go away when windows */
- gShowingSecHandle = nil;
- if ((*shortname)->boxHandle != nil)
- TEActivate((*shortname)->boxHandle);
- if (twindow == gClipWindowPtr) {
- SetWMenus(false);
- }
- /* change plane */
- }
-
- /* end ChangePlane */
-
- Boolean IsAppWindow(WindowPtr window)
- {
- short windowKind;
- if (window == nil)
- return false;
- else { /* application windows have windowKinds = userKind (8) */
- windowKind = ((WindowPeek)window)->windowKind;
- return(windowKind = userKind);
- }
- }
-
- /* end IsAppWindow*/
-
- /* Check to see if a window belongs to a desk accessory. */
-
- Boolean IsDAWindow(WindowPtr window)
- {
- if (window == nil)
- return false;
- else /* DA windows have negative windowKinds */
- return((WindowPeek)window)->windowKind < 0;
- }
-
- /* end IsDAWindow*/
-
- void DrawClip(WindowPtr windowIn)
- {
- Str63 clipString;
- MoveTo(10, 10);
- GetIndString(clipString, kClipBoardStrings, 1);
- DrawString(clipString);
- GetIndString(clipString, kClipBoardStrings, gClipHasContents);
- DrawString(clipString);
- MoveTo(0, 13);
- LineTo(gClipWindowPtr->portRect.right, 13);
- HLock(gScrapData);
- if ((GetHandleSize(gScrapData) != 0) && gScrapData) {
- Rect picRect;
- picRect = ((*(PicHandle)gScrapData))->picFrame;
- OffsetRect(&picRect, 0, 15);
- DrawPicture((PicHandle)gScrapData, &picRect);
- }
- HUnlock(gScrapData);
-
- }
-
- /* end DrawClip */
-
- void ClipClick(WindowPtr twindow)
- { /* nothing happens on a clipboard click, it's in here for whenever I put scroll bars in */
- }
- void CloseClip(void)
- {
- ChangePlane(gClipWindowPtr);
- HideWindow(gClipWindowPtr);
- if (FrontWindow() != nil)
- ChangePlane(FrontWindow());
- SetItem(gEditMenuHandle, kClapNum, "\pShow Clipboard");
- }
-
- void UpdateScrap(void)
- {
-
- Handle textData;
- long myOffset;
- long length;
- textData = NewHandle(0);
- if (gClipHasContents == kClipHasSub) {
- } else {
- /* show standard scrap types, prefering pictures */
- length = GetScrap(gScrapData, 'PICT', &myOffset);
- if (length < 0) {
- if (length == noTypeErr) {
- length = GetScrap(textData, 'TEXT', &myOffset);
- /* if it's text, I'm going to make it into a picture for my clipboard display, makes things */
- /* easier in my draw proc */
- if (length > 0) {
- DisposHandle(gScrapData); /* kill the old one */
- gScrapData = (Handle)OpenPicture(&gClipWindowPtr->portRect);
- HLock(textData);
- TextBox(*textData, length, &gClipWindowPtr->portRect, teJustLeft);
- HUnlock(textData);
- ClosePicture();
- gClipHasContents = kClipHasText;
- }
- } else { /* another type of error, like nothing available at all */
- MySetHandleSize(gScrapData, 0);
- gClipHasContents = kClipEmpty;
- }
- } else {
- /* set the pict rect so it's in the upper left of our clipboard */
- gClipHasContents = kClipHasPict;
- OffsetRect(&((*(PicHandle)gScrapData))->picFrame, (((*(PicHandle)gScrapData))->picFrame.left * -1),
- (((*(PicHandle)gScrapData))->picFrame.top * -1) + 11);
- }
- }
- DisposHandle(textData);
- if (((WindowPeek)gClipWindowPtr)->visible) {
- WindowPtr temp;
- GetPort(&temp);
- SetPort(gClipWindowPtr);
- InvalRect(&gClipWindowPtr->portRect);
- SetPort(temp);
- }
- }
-
- /* end updatescrap */
-
- /* SpitClip spits our private subscription out to the general clipboard as a PICT type scrap */
- /* when we go in the background. Does nothing if it's not a subscription, since it'll already */
- /* be right. */
-
-
- void SpitClip(void)
- {
- if (gClipHasContents == kClipHasSub) {
- ZeroScrap();
- HLock(gScrapData);
- PutScrap(GetHandleSize(gScrapData), 'PICT', *gScrapData);
-
- }
- }
-
-
- #undef __MYWINDOWS__
-